import { IUserInfo } from '../controllers/User';
import { UserModel } from '../db';

async function getUserList() {
  return await UserModel.find();
}

async function addUser(userInfo: IUserInfo) {
  return await UserModel.create(userInfo);
}

async function getUser(username: string) {
  return await UserModel.findOne({ username });
}

async function getProfile(id: string) {
  const user = await UserModel.findOne({ _id: id });
  if (user) {
    return {
      uid: user._id,
      username: user.username,
      level: user.level
    };
  } else {
    return null;
  }
}

export default {
  getUserList,
  addUser,
  getUser,
  getProfile
};
